/**
 * 
 */
package poc.android.model;

import java.util.Map;

import org.json.JSONObject;

/**
 * @author Jason
 *
 */
public class Role extends AbstractJSonModel {
	
	public static Role[] availableRoles=new Role[3];
	static {
		availableRoles[0]=new Role();
		availableRoles[0].id=Integer.valueOf(1);
		availableRoles[0].description="student";
		
		availableRoles[1]=new Role();
		availableRoles[1].id=Integer.valueOf(2);
		availableRoles[1].description="teacher";
		
		availableRoles[2]=new Role();
		availableRoles[2].id=Integer.valueOf(3);
		availableRoles[2].description="principle";
	}
	
	Integer id;
	String description;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public JSONObject toJSon(Map<String, Object> pConfig) {
		JSONObject aJObj=null;
		
		if (this.id!=null) {
			try {
				aJObj=new JSONObject();
				aJObj.put(ID_FIELD, this.getClass().getCanonicalName());
				aJObj.put("id", this.id);
				aJObj.put("description", this.description);
				
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException("[Role][toJSon]", e);
			}
		}
		return aJObj;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public static Object toObjectFromJSonString(String pJSonString) {
		Object aObj=null;
		
		try {
			if (pJSonString!=null && pJSonString.trim().length()>0) {
				JSONObject aJObj=new JSONObject(pJSonString);
				aObj=Role.toObjectFromJSonObject(aJObj);
			}
		} catch(RuntimeException re) {
			throw re;
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("[Role][toObjectFromJSonString]", e);
		}
		return aObj;
	}
	/**
	 * {@inheritDoc}
	 */
	public static Object toObjectFromJSonObject(JSONObject pJSonObject) {
		Role aObj=null;
		
		if (pJSonObject!=null) {
			String aVal=null;
			
			try {
				aVal=pJSonObject.getString(ID_FIELD);
				if (aVal!=null && aVal.equals(Role.class.getCanonicalName())) {
					aObj=new Role();
					aObj.setId(Integer.valueOf(pJSonObject.getString("id")));
					aObj.setDescription(pJSonObject.getString("description"));
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException("[Role][toObjectFromJSonObject]", e);
			}
		}
		return aObj;
	}
}
